package com.tuling.netty.nettyDemo;

import com.tuling.netty.splitpacket.MyMessagePropol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class MyMessageDecode extends ByteToMessageDecoder {
    int length = 0;
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("客户端调用服务端数据");
        if(byteBuf.readableBytes() > 4){
            if(length == 0 ){
                length = byteBuf.readInt();
            }
            if(byteBuf.readableBytes() < length){
                System.out.println("目前数据可读长度不够请继续等待！");
                return;
            }
            byte[] content =  new byte[length];
            if(byteBuf.readableBytes() >= length){
                byteBuf.readBytes(content);
                MyMessagePropol myMessagePropol = new MyMessagePropol();
                myMessagePropol.setLen(length);
                myMessagePropol.setContent(content);
            }
        }

        length = 0;
    }
}
